package com.senpai.database;

import java.util.LinkedList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySQLiteHelper extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "Mydatabase";

	public MySQLiteHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);

	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String CREATE_TABLE = "CREATE TABLE reponse ( "
				+ "id INTEGER PRIMARY KEY, " + "num TEXT," + "question TEXT, "
				+ "category TEXT, " + "date TEXT)";

		db.execSQL(CREATE_TABLE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS reponse");
		this.onCreate(db);

	}

	private static final String TABLE_REPONSE = "reponse";
	private static final String KEY_ID = "id";
	private static final String KEY_NUM = "num";
	private static final String KEY_CATEGORY = "question";
	private static final String KEY_QUESTION = "category";
	private static final String KEY_DATE = "date";

	private static final String[] COLUMNS = { KEY_ID, KEY_NUM,
			KEY_QUESTION, KEY_CATEGORY, KEY_DATE };

	public void addQuestion(Question question) {
		SQLiteDatabase db = this.getWritableDatabase();

		ContentValues values = new ContentValues();
		
		values.put(KEY_ID, question.getId());
		values.put(KEY_NUM, question.getNum());
		values.put(KEY_QUESTION, question.getQuestion());
		values.put(KEY_CATEGORY, question.getCategory());
		values.put(KEY_DATE, question.getDate());

		db.insert(TABLE_REPONSE, null, values);
		db.close();
	}

	public Question getQuestion(int id) {

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_REPONSE, COLUMNS, " id = ?",
				new String[] { String.valueOf(id) }, null, null, null, null);

		if (cursor != null)
			cursor.moveToFirst();

		Question question = new Question();
		question.setId(Integer.parseInt(cursor.getString(0)));
		question.setNum(cursor.getString(1));
		question.setCategory(cursor.getString(2));
		question.setQuestion(cursor.getString(3));
		question.setDate(cursor.getString(4));

		return question;
	}

	public Question getQuestionById(String id) {

		SQLiteDatabase db = this.getReadableDatabase();
		Cursor cursor = db.query(TABLE_REPONSE, COLUMNS, " num = ?",
				new String[] { String.valueOf(id) }, null, null, null, null);

		if (cursor != null)
			cursor.moveToFirst();

		Question question = new Question();
		question.setId(Integer.parseInt(cursor.getString(0)));
		question.setNum(cursor.getString(1));
		question.setCategory(cursor.getString(2));
		question.setQuestion(cursor.getString(3));
		question.setDate(cursor.getString(4));

		return question;
	}

	public List<Question> getAllQuestions() {
		List<Question> questionList = new LinkedList<Question>();

		String query = "SELECT  * FROM " + TABLE_REPONSE;

		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.rawQuery(query, null);

		Question question = null;
		if (cursor.moveToFirst()) {
			do {
				question = new Question();
				question.setId(Integer.parseInt(cursor.getString(0)));
				question.setNum(cursor.getString(1));
				question.setCategory(cursor.getString(2));
				question.setQuestion(cursor.getString(3));
				question.setDate(cursor.getString(4));
				questionList.add(question);
			} while (cursor.moveToNext());
		}

		return questionList;
	}

}
